/**
 * 
 */
package cn.gov.msa.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import cn.gov.msa.dao.QuestionMapper;
import cn.gov.msa.model.Question;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.QuestionService;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */
public class QuestionServiceImpl implements QuestionService {


	@Autowired
	private QuestionMapper questionMapper;
	
	
	@Transactional
	@Override
	public List<Question> selectData(int pageNum, int pageSize, Question question) {
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码 		
		return questionMapper.selectData(question);
	}
	
	@Transactional
	@Override
	public List<Question> selectDataq(int pageNum, int pageSize, Question question) {
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码 		
		return questionMapper.selectDataq(question);
	}

	@Transactional
	@Override
	public int updateState(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.updateState(question);
	}
	
	@Transactional
	@Override
	public int insertW(Question question) {
		return questionMapper.insertW(question);
	}

	@Transactional
	@Override
	public int insertq(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.insertq(question);
	}

	@Transactional
	@Override
	public int updateq(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.updateq(question);
	}

	@Transactional
	@Override
	public int insertItem(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.insertItem(question);
	}

	@Transactional
	@Override
	public int deleteItem(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.deleteItem(question);
	}

	@Override
	public int deleteq(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.deleteq(question);
	}

	@Transactional
	@Override
	public int delete(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.delete(question);
	}

	@Transactional
	@Override
	public int update(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.update(question);
	}

	@Transactional
	@Override
	public int updateItemNum(Question question) {
		// TODO Auto-generated method stub
		return questionMapper.updateItemNum(question);
	}

	@Transactional
	@Override
	public JSONObject insert(JSONObject param) {
		JSONArray jsonArray = param.getJSONArray("questions");
		param.discard("questions");

		Question question1 = GsonHelper.convertEntity(param.toString(), Question.class);
		int result = this.insertW(question1);
		System.err.println(question1.getQUESTIONNAIREID());
		if(result > 0){
			for (int i = 0; i < jsonArray.size(); i++) {
				Question question2 = GsonHelper.convertEntity(jsonArray.getJSONObject(i).toString(), Question.class);
				question2.setQUESTIONNAIREID(question1.getQUESTIONNAIREID());
				result = this.insertq(question2);
				if(result > 0){
					if(question2.getQUESTIONITEM().indexOf(",")==-1) {
						question2.setNUM("1");
						this.insertItem(question2);
					}else {
						String[] items=question2.getQUESTIONITEM().split(",");
						for (int j = 0; j < items.length; j++) {
							question2.setQUESTIONITEM(items[j]);
							question2.setNUM((j+1)+"");
							result = this.insertItem(question2);
						}			
					}
				}
			}
		}
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", 0);
		if(result > 0){
			jsonResult.put("result", 1);
			jsonResult.put("info", "数据保存成功");		
			jsonResult.put("pk",question1.getQUESTIONNAIREID());
		}
		return jsonResult;
	}
	
	@Transactional
	@Override
	public JSONObject updateData(JSONObject param) {
		JSONArray jsonArray = param.getJSONArray("questions");
		param.discard("questions");

		Question question1 = GsonHelper.convertEntity(param.toString(), Question.class);
		int result = this.update(question1);

		this.deleteItem(question1);
		this.deleteq(question1);
		
		for (int i = 0; i < jsonArray.size(); i++) {
			Question question2 = GsonHelper.convertEntity(jsonArray.getJSONObject(i).toString(), Question.class);
			question2.setQUESTIONNAIREID(question1.getQUESTIONNAIREID());
			result = this.insertq(question2);
			if(result > 0){
				if(question2.getQUESTIONITEM().indexOf(",")==-1) {
					question2.setNUM("1");
					this.insertItem(question2);
				}else {
					String[] items=question2.getQUESTIONITEM().split(",");
					for (int j = 0; j < items.length; j++) {
						question2.setQUESTIONITEM(items[j]);
						question2.setNUM((j+1)+"");
						result = this.insertItem(question2);
					}			
				}
			}
		}
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", 0);
		if(result > 0){
			jsonResult.put("result", 1);
			jsonResult.put("info", "数据更新成功");
			jsonResult.put("pk",question1.getQUESTIONNAIREID());
		}
		return jsonResult;
	}

}
